<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .wrapper {
            width: 400px;
            height: 250px;
            margin: 100px auto 0;
            overflow: hidden;
            position: relative;
        }

        .sliderPage {
            width: 2000px;
            height: 250px;
            position: absolute;
            left: 0;
        }

        .sliderPage li {
            width: 400px;
            height: 250;
            float: left;

        }

        .sliderPage li img {
            width: 100%;
            height: 100%;

        }

        .btn {
            width: 40px;
            height: 40px;
            text-align: center;
            background-color: black;
            position: absolute;
            line-height: 40px;
            color: #fff;
            top: 50%;
            margin-top: -25px;
            opacity: 0.7;
        }

        .wrapper:hover .btn {
            cursor: pointer;
            opacity: 1;
        }

        .leftBtn {
            left: 15px;

        }

        .rightBtn {
            right: 15px;
        }

        .sliderIndex {
            position: absolute;
            width: 100%;
            text-align: center;
            bottom: 15px;
        }

        .sliderIndex span {
            width: 8px;
            height: 8px;
            background-color: #ccc;
            border-radius: 50%;
            margin-right: 10px;
            display: inline-block;
            cursor: pointer;
        }

        .sliderIndex .active {
            background-color: #f40;
        }
    </style>
</head>

<body>
    <div class="wrapper">
        <ul class="sliderPage">
            <li><img src="img/img1.jpg" alt=""></li>
            <li><img src="img/img2.jpg" alt=""></li>
            <li><img src="img/img3.jpg" alt=""></li>
            <li><img src="img/img1.jpg" alt=""></li>
        </ul>
        <div class="btn leftBtn">&lt;</div>
        <div class="btn rightBtn">&gt;</div>
        <div class="sliderIndex">
            <span class="active"></span>
            <span></span>
            <span></span>
        </div>
    </div>
    <script src="sport框架.js"></script>
    <script>
        var timer = null;
        var sliderPage = document.getElementsByClassName('sliderPage')[0];
        var moveWidth = sliderPage.children[0].offsetWidth;
        var num = sliderPage.children.length - 1;
        var leftBtn = document.getElementsByClassName('leftBtn')[0];
        var rightBtn = document.getElementsByClassName('rightBtn')[0];
        var lock = true;
        var index = 0;
        var aSpan = document.getElementsByClassName('sliderIndex')[0].getElementsByTagName('span');

        leftBtn.onclick = function () {
            autoMove('right->left');
        }
        rightBtn.onclick = function () {
            autoMove('left->right');
        }

        for(var i = 0;i < aSpan.length;i ++) {
            (function (myIndex) {
                aSpan[i].onclick = function () {
                    clock = false;
                    clearInterval(timer);
                    index = myIndex;
                    startMove(sliderPage,{left:- index * moveWidth},function () {
                        lock = true;
                        timer = setTimeout(autoMove,1500);
                        changIndex(index);
                    })
                }
            } (i))
        }
        
        function autoMove(direction) {
            if (lock) {
                lock = false;
                clearInterval(timer);
                if (!direction || direction == 'left->right') {//默认从右向左
                    index ++;
                    startMove(sliderPage, { left: sliderPage.offsetLeft - moveWidth }, function () {
                        if (sliderPage.offsetLeft == -num * moveWidth) {
                            sliderPage.style.left = '0px';
                            index = 0;
                        }
                        timer = setInterval(autoMove, 1500);
                        lock = true;
                        changIndex(index);
                    })
                } else if (direction == 'right->left') {
                    if (sliderPage.offsetLeft == 0) {
                        sliderPage.style.left = -num * moveWidth + 'px';
                        index = num;
                    }
                    index --;
                    startMove(sliderPage, { left: sliderPage.offsetLeft + moveWidth }, function () {
                        timer = setInterval(autoMove, 1500);
                        lock = true;
                        changIndex(index);
                    });

                }
            }

        }
        
        
        function changIndex (index) {
            for(var i = 0;i < aSpan.length;i ++) {
                aSpan[i].className = '';
            }
            aSpan[index].className = 'active';
        }
        timer = setTimeout(autoMove, 1500);
    </script>
</body>

</html>